package com.marvels.product.param.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dao.McptManagementFeeMapper;
import com.marvels.product.param.dto.McptConsultingFeeDTO;
import com.marvels.product.param.dto.McptManagementFeeDTO;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.MngFeeService;
@Service
public class MngFeeServiceImpl implements MngFeeService {
	@Autowired
	private McptManagementFeeMapper mcptManagementFeeMapper;
	
	@Override
	public PageInfo<McptManagementFeeDTO> getPageInfoByParam(Map<String, Object> param) throws MarvelsBusinessException{
		Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptManagementFeeDTO>  mcptManagementFeeList = mcptManagementFeeMapper.getListByParam(param);
		
		PageInfo<McptManagementFeeDTO> pageInfo = new PageInfo<McptManagementFeeDTO>(mcptManagementFeeList);
	    
		return pageInfo;
	}

	@Override
	public McptManagementFeeDTO getEntityByPrimaryKey(Long managementFeeId) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptManagementFeeMapper.selectByPrimaryKey(managementFeeId);
		
	}

	@Override
	public int insert(McptManagementFeeDTO dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		//return ;
		return mcptManagementFeeMapper.insert(dto);
	}

	@Override
	public int updateByPrimaryKey(McptManagementFeeDTO dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptManagementFeeMapper.updateByPrimaryKey(dto);
	}

	@Override
	public int updateStatus(McptManagementFeeDTO dto) throws MarvelsBusinessException{
		Integer count = 0;
		try {
			count = mcptManagementFeeMapper.updateStatus(assemblyParam(dto));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>管理费审核异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}

	@Override
	public McptManagementFeeDTO getManagementFeeByManageFeeCode(McptProduct dto) throws MarvelsBusinessException {
		return mcptManagementFeeMapper.selectManagementFeeByManageFeeCode(dto);
	}

	@Override
	public McptManagementFeeDTO selectByProductInfo(McptProduct product){
		return mcptManagementFeeMapper.selectByProductInfo(product);
	}

	public int deleteByPrimaryKey(McptManagementFeeDTO dto) throws MarvelsBusinessException {
		Integer count = 0;
		try {
			count = mcptManagementFeeMapper.deleteByPrimaryKey(assemblyParam(dto));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>管理费删除异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}
	/**
	 * 组装参数
	 * @param record
	 * @return
	 * @throws Exception
	 */
	private McptManagementFeeDTO assemblyParam(McptManagementFeeDTO record) throws Exception{
		String userName = MarvelsConstant.SYSTEM_USER;
		 UserDTO user = CacheUtils.getCacheUser();
		 if(user!=null){
			 userName = user.getUserName();
		 }
		 record.setUpdatedBy(userName);
		 return record;
	}
}
